package com.zou.lotterylucky.activity

import android.content.Intent
import android.os.Bundle
import android.text.InputType
import android.widget.Toast
import com.elvishew.xlog.XLog
import com.qmuiteam.qmui.widget.dialog.QMUIDialog
import com.qmuiteam.qmui.widget.grouplist.QMUICommonListItemView
import com.qmuiteam.qmui.widget.grouplist.QMUIGroupListView
import com.zou.lotterylucky.Constants
import com.zou.lotterylucky.R
import com.zou.lotterylucky.utils.SPUtils
import com.zou.lotterylucky.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_setting_outer.*

class SettingOuterActivity : BaseActivity() {
    private lateinit var item2: QMUICommonListItemView
    private lateinit var item3: QMUICommonListItemView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting_outer)
        topbar.setTitle(R.string.waibushezhi)
        topbar.addLeftBackImageButton().setOnClickListener { onBackPressed() }

        val storeName = SPUtils[this, "store_name", "Lucky"] as String
        storeNameTxt.setText(storeName)

        saveBtn.setOnClickListener {
            if (storeNameTxt.text.isEmpty()) {
                ToastUtil.showMsg(this, R.string.input_store)
                return@setOnClickListener
            }

            SPUtils[this, "store_name"] = storeNameTxt.text.toString()
            ToastUtil.showMsg(this, "保存成功")
        }

        item2 = qlv.createItemView(getString(R.string.dayinzhangshu))
        item2.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON

        item3 = qlv.createItemView(getString(R.string.chooseLang))
        item3.accessoryType = QMUICommonListItemView.ACCESSORY_TYPE_CHEVRON

        QMUIGroupListView.newSection(this)
            .addItemView(item2) {
                val builder = QMUIDialog.EditTextDialogBuilder(this)
                builder.setTitle(getString(R.string.shezhi))
                    .setPlaceholder(getString(R.string.qingshurumima))
                    .setInputType(InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD)
                    .addAction(R.string.cancel) { dialog, _ -> dialog.dismiss() }
                    .addAction(R.string.confirm) { dialog, _ ->
                        val text = builder.editText.text
                        if (text != null && text.isNotEmpty()) {
                            //                                Toast.makeText(this, "您的昵称: $text", Toast.LENGTH_SHORT).show()
                            val psd =
                                SPUtils.get(
                                    this,
                                    Constants.SP_PASSWORD,
                                    Constants.print_psd
                                ) as String
                            if (text.toString() == psd) {
                                selectPrintSize()
                                dialog.dismiss()
                            } else {
                                Toast.makeText(
                                    this,
                                    getString(R.string.password_error),
                                    Toast.LENGTH_SHORT
                                )
                                    .show()
                            }

                        } else {
                            Toast.makeText(
                                this,
                                getString(R.string.please_input_password),
                                Toast.LENGTH_SHORT
                            ).show()
                        }
                    }
                    .create().show()

            }.addItemView(item3) {
                val current = SPUtils.get(this, Constants.SP_Lang, 1) as Int
                val opts = arrayOf("Spanish", "中文", "English")
                QMUIDialog.CheckableDialogBuilder(this)
                    .setCheckedIndex(current)
                    .addItems(opts) { d, index ->
                        if (index != current) {
                            d.dismiss()
                            SPUtils.set(this, Constants.SP_Lang, index)
                            val intent = Intent(
                                this,
                                MainActivity::class.java
                            )
                            intent.flags =
                                Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
                            startActivity(intent)

                            android.os.Process.killProcess(android.os.Process.myPid())
                            System.exit(0)
                        }
                    }.create().show()
            }.addTo(qlv)

        initData()
    }

    private fun selectPrintSize() {
        val current = SPUtils.get(this, Constants.SP_DaYinZhangShu, 0) as Int
        val opts = arrayOf(getString(R.string.dayinkehu), getString(R.string.dayinkehujiacungen))
        QMUIDialog.CheckableDialogBuilder(this)
            .setCheckedIndex(current)
            .addItems(opts) { d, index ->
                XLog.e(index)
                if (index != current) {
                    SPUtils.set(this, Constants.SP_DaYinZhangShu, index)
                    d.dismiss()
                    item2.detailText =
                        if (index == 0) getString(R.string.dayinkehu) else getString(R.string.dayinkehujiacungen)
                } else {
                    d.dismiss()
                }
            }.create().show()
    }

    private fun initData() {

        val dyzs = SPUtils.get(this, Constants.SP_DaYinZhangShu, 0) as Int
        item2.detailText =
            if (dyzs == 0) getString(R.string.dayinkehu) else getString(R.string.dayinkehujiacungen)
    }
}
